__author__ = 'el_even'

from random import randint
# todo: refactor code and move ^randomizer^ to other section from delmovie
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.expected_conditions import *
from selenium.webdriver.common.by import By

from time import sleep

from php4dvd.pages.loginpage import LoginPage
from php4dvd.pages.internalpage import InternalPage
from php4dvd.pages.userprofilepage import UserProfilePage
from php4dvd.pages.addmoviepage import AddMoviePage
from php4dvd.pages.homepage import HomePage
from php4dvd.pages.moviepage import MoviePage
from php4dvd.model.userdata import UserData
from php4dvd.model.moviedata import MovieData


class Application(object):
    """
    Describes application logic
    """

    def __init__(self, driver, base_url):
        driver.get(base_url)
        self.wait = WebDriverWait(driver, 10)
        self.lp = LoginPage(driver, base_url)
        self.ip = InternalPage(driver, base_url)
        self.upp = UserProfilePage(driver, base_url)
        self.hp = HomePage(driver, base_url)
        self.amp = AddMoviePage(driver, base_url)
        self.mp = MoviePage(driver, base_url)
        self.md = MovieData()

    def login(self, user):
        self.lp.username_field.clear()
        self.lp.username_field.send_keys(user.username)
        self.lp.password_field.clear()
        self.lp.password_field.send_keys(user.password)
        self.lp.submit_button.click()

    def logout(self):
        self.ip.logout_button.click()
        self.wait.until(alert_is_present()).accept()

    def is_logged_in(self):
        return self.ip.is_this_page

    def is_not_logged_in(self):
        return self.lp.is_this_page

    def is_logged_in_as(self, user):
        return self.is_logged_in() and self.get_logged_user().username == user.username

    def get_logged_user(self):
        self.ip.user_profile_link.click()
        return UserData(username=self.upp.username_field.get_attribute("value"),
                        email=self.upp.email_field.get_attribute("value"))

    def ensure_login_as(self, user):
        element = self.wait.until(presence_of_element_located((By.CSS_SELECTOR, "nav, #loginform")))
        if element.tag_name == "nav":
            if self.is_logged_in_as(user):
                return
            else:
                self.logout()
        self.login(user)

    def ensure_logout(self):
        element = self.wait.until(presence_of_element_located((By.CSS_SELECTOR, "nav, #loginform")))
        if element.tag_name == "nav":
            self.logout()

    def add_movie(self, style):
        self.hp.add_button.click()
        self.amp.movie_title_input.clear()
        self.amp.movie_title_input.send_keys(style.title)
        self.amp.imdb_id_input.clear()
        self.amp.imdb_id_input.send_keys(style.imdb_id)
        self.amp.aka_textarea.clear()
        self.amp.aka_textarea.send_keys(style.aka)
        self.amp.year_input.clear()
        self.amp.year_input.send_keys(style.year)
        # todo: (not necessary) add more input data
        self.amp.submit_button.click()

    def ensure_add_movie(self):
        return self.wait.until(text_to_be_present_in_element((By.CSS_SELECTOR, ".maininfo_full>h2"),
                                                             self.md.movie_title))

    def is_movie_added(self):
        return self.mp.is_this_page

    def is_add_error(self):
        if self.amp.wait.until(presence_of_element_located((By.XPATH, ".//label[@class='error']"))) != "":
            return True
        else:
            return False

    def del_movie(self):
        del_movie_id = randint(0, len(self.hp.movies)-1)
        self.hp.movies[del_movie_id].click()
        self.mp.del_button.click()
        self.wait.until(alert_is_present()).accept()

    def is_movie_deleted(self):
        # todo: make sweeter deletion check, using del_movie_id
        return self.mp.is_this_page

    def search(self, query):
        self.hp.search_input.clear()
        self.hp.search_input.send_keys(query)
        sleep(1)  # to ensure ENTER key pressed
        self.hp.search_input.send_keys(Keys.ENTER)
        sleep(1)  # wait a little bit more because of my notebook slowpokeness
        self.wait.until(presence_of_element_located((By.XPATH, ".//div[@id='results']")))

    def found_items(self):
        return len(self.hp.movies)

    def is_found_nothing(self):
        if self.wait.until(presence_of_element_located((By.CSS_SELECTOR, ".content"))) != "":
            return True
        else:
            return False
